package cn.mini.mybatis.mybatis.executor;

import cn.mini.mybatis.mybatis.executor.resultset.ResultSetHandler;
import cn.mini.mybatis.mybatis.executor.statementHandler.StatementHandler;
import cn.mini.mybatis.mybatis.jdbc.MySQLConnectionUtil;
import cn.mini.mybatis.mybatis.mapping.BoundSql;
import cn.mini.mybatis.mybatis.mapping.MappedStatement;
import cn.mini.mybatis.mybatis.session.Configuration;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class SimpleExecutor extends BaseExecutor{

    public SimpleExecutor(Configuration configuration) {
        super(configuration);
    }



    @Override
    protected <E> List<E> doQuery(MappedStatement ms, Object parameter, ResultSetHandler resultHandler, BoundSql boundSql)  {
        try {
            Configuration configuration = ms.getConfiguration();
            // staementHandler 中保存有信息
            StatementHandler handler = configuration.newStatementHandler(this, ms, parameter,
                    resultHandler, boundSql);
            Connection connection = MySQLConnectionUtil.getConnection();
            Statement stmt = handler.prepare(connection);
            handler.parameterize(stmt,boundSql);
            return handler.query(stmt,resultHandler);
        }catch (SQLException exception) {
            return null;
        }
    }
}
